﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DALFactory;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class SysCodeBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly ISysCodeDA sysCodeDA = DataAccess.CreateSysCodeDA();
        private static readonly ISysCodeDetailDA sysCodeDetailDA = DataAccess.CreateSysCodeDetailDA();

        public SysCodeBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public SysCodeBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> SysCodeType)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<SysCodeInfo> sysCodeInfos = new List<SysCodeInfo>();
                SysCodeInfo sysCodeInfo = null;
                foreach (string id in SysCodeType)
                {
                    sysCodeInfo = sysCodeDA.GetSysCodeByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    sysCodeInfo.SysCodeDetailInfo = sysCodeDetailDA.GetSysCodeDetailByID(dataBase, tran, id, this.dataBaseID[0]);
                    sysCodeInfos.Add(sysCodeInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                sysCodeDetailDA.DeleteSysCodeDetail(dataBase, tran, SysCodeType, this.dataBaseID);
                if (sysCodeDA.DeleteSysCode(dataBase, tran, SysCodeType, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < SysCodeType.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<SysCodeInfo>(dataBase, tran, sysCodeInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "SYS_CODE", this.dataBaseID);
                        if ((sysCodeInfos[i].SysCodeDetailInfo != null) && (sysCodeInfos[i].SysCodeDetailInfo.Count > 0))
                        {
                            for (int j = 0; j < sysCodeInfos[i].SysCodeDetailInfo.Count; j++)
                            {
                                auditDataBLL.InsertAuditData<SysCodeDetailInfo>(dataBase, tran, sysCodeInfos[i].SysCodeDetailInfo[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "SYS_CODE_DETAIL", this.dataBaseID);
                            }
                        }
                        temp = temp + SysCodeType[i] + ",";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return sysCodeDA.GetAllSysCode(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            SysCodeInfo SysCode = new SysCodeInfo();
            SysCode = sysCodeDA.GetSysCodeByID(id, this.dataBaseID[0], this.pubParasInfo);
            SysCode.SysCodeDetailInfo = DataAccess.CreateSysCodeDetailDA().GetSysCodeDetailByID(id, this.dataBaseID[0]);
            return SysCode;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            SysCodeQueryEntity SysCodeQuery = obj as SysCodeQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(SysCodeQuery.SysCodeType))
            {
                sqlWhere.Add(" SYS_CODE_TYPE " + SysCodeQuery.SysCodeType);
            }
            if (!string.IsNullOrEmpty(SysCodeQuery.Description))
            {
                sqlWhere.Add(" T.[TEXT] " + SysCodeQuery.Description);
            }
            return sysCodeDA.GetSysCodeByQueryList(sqlWhere, SysCodeQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string StrSysCode = string.Empty;
            try
            {
                SysCodeInfo sysCodeInfo = obj as SysCodeInfo;
                if (sysCodeDA.CheckSysCodeIDUnique(dataBase, tran, sysCodeInfo.SysCodeType, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F1800_001", this.pubParasInfo.UserLanguage, new List<string> { sysCodeInfo.SysCodeType });
                }
                sysCodeInfo.CreatedDate = DateTime.Now;
                sysCodeInfo.UpdatedDate = DateTime.Now;
                sysCodeInfo.UpdatedBy = sysCodeInfo.CreatedBy;
                sysCodeDA.InsertSysCode(dataBase, tran, sysCodeInfo, this.dataBaseID);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<SysCodeInfo>(dataBase, tran, null, sysCodeInfo, AuditAction.Insert, this.pubParasInfo.UserID, "SYS_CODE", this.dataBaseID);
                StrSysCode = sysCodeInfo.SysCodeType;
                if ((sysCodeInfo.SysCodeDetailInfo != null) && (sysCodeInfo.SysCodeDetailInfo.Count > 0))
                {
                    for (int i = 0; i < sysCodeInfo.SysCodeDetailInfo.Count; i++)
                    {
                        if (sysCodeDetailDA.CheckSysCodeDetailIDUnique(dataBase, tran, sysCodeInfo.SysCodeDetailInfo[i].SysCode, sysCodeInfo.SysCodeDetailInfo[i].SysCodeType, this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F1800_001", this.pubParasInfo.UserLanguage, new List<string> { sysCodeInfo.SysCodeDetailInfo[i].SysCode });
                        }
                        else
                        {
                            sysCodeDetailDA.InsertSysCodeDetail(dataBase, tran, sysCodeInfo.SysCodeDetailInfo[i], this.dataBaseID);
                            auditDataBLL.InsertAuditData<SysCodeDetailInfo>(dataBase, tran, null, sysCodeInfo.SysCodeDetailInfo[i], AuditAction.Insert, this.pubParasInfo.UserID, "SYS_CODE", this.dataBaseID);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return StrSysCode;
        }

        public string Update(object obj)
        {
            string StrSysCode = string.Empty;
            try
            {
                SysCodeInfo sysCodeInfo = obj as SysCodeInfo;
                sysCodeInfo.UpdatedDate = DateTime.Now;
                SysCodeInfo oldSysCodeInfo = sysCodeDA.GetSysCodeByID(sysCodeInfo.SysCodeType, this.dataBaseID[0], this.pubParasInfo);
                if (sysCodeDA.UpdateSysCode(sysCodeInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<SysCodeInfo>(oldSysCodeInfo, sysCodeInfo, AuditAction.Update, this.pubParasInfo.UserID, "SYS_CODE", this.dataBaseID[0]);
                    StrSysCode = sysCodeInfo.SysCodeType;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return StrSysCode;
        }
    }
}

